--
-- Author: mm
-- Date: 2018-09-22 10:59:30
--
-- CorepropRender
--

local PropNameBtnRender = app.mvc.createRenderClass(...)
local CorePropUIHelp = require("app.coreProp.CorePropUIHelp")
--data 
function PropNameBtnRender.createWithType(renderType, node)
    local sprite = PropNameBtnRender.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(renderType)
    return sprite
end

function PropNameBtnRender:init(renderType)
    self.super.init(self, nil)
    self._type = renderType
    self._isSelected = nil
    self._ui["Panel/icon"]:loadTexture(CorePropUIHelp.getIconByType(renderType), ccui.TextureResType.plistType)
    self._ui["Panel/tlt"]:setString(CorePropUIHelp.getTextByType(renderType))

    display.uiAddClick( self._ui["Panel"], function() -- 立即开始
        if self._isSelected then
            return
        end
        self:setSelected(true)
        executeFunc(self._onClick, self._type)
    end)
    -- self._ui["Panel/Image"]:setAnchorPoint(cc.p(0, 0.5))
    self._oldTextColor = self._ui["Panel/num_1"]:getTextColor()
    self:refresh()
end

function PropNameBtnRender:getType()
    return self._type
end

function PropNameBtnRender:refresh()
    if not self._data then
        return
    end
    self._ui["Panel/num_1"]:setString(self._data.value)
    if self._data.canNotAdd then
        self._ui["Panel/num_1"]:setTextColor(display.COLOR_RED)
    else
        self._ui["Panel/num_1"]:setTextColor(self._oldTextColor)
    end
    self._ui["Panel/num_2"]:setString(self._data.mountAdd or 0)

    
    -- 设置数值的位置
    local num2PosX = self._ui["Panel/num_2"]:getPositionX()
    local num2Width = self._ui["Panel/num_2"]:getContentSize().width
    local addWidth = self._ui["Panel/0.5"]:getContentSize().width
    self._ui["Panel/0.5"]:setPositionX(num2PosX - num2Width/2 - addWidth/2 - 3)
end

function PropNameBtnRender:refreshRedPoint(b)
    self._ui.tishi:setVisible(b)
end

function PropNameBtnRender:setSelected(trueOrFalse)
    if self._isSelected == trueOrFalse then
        return
    end
    self._isSelected = trueOrFalse
    if self._isSelected then
        self._ui["Panel/bg_n"]:setVisible(false)
        self._ui["Panel/bg_h"]:setVisible(true)
    else
        self._ui["Panel/bg_n"]:setVisible(true)
        self._ui["Panel/bg_h"]:setVisible(false)
    end
end

function PropNameBtnRender:setSelectedByType(tp)
    self:setSelected(tp == self._type)
end

function PropNameBtnRender:onBtnClick(cb)
    self._onClick = cb
end


return PropNameBtnRender
